package com.thenie.controller.admin;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.thenie.pojo.RoomType;
import com.thenie.pojo.User;
import com.thenie.service.UserService;
import com.thenie.utils.DataGridViewResult;
import com.thenie.utils.SystemConstant;
import com.thenie.vo.UserVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Thenie
 * @date 2023/5/18 19:51
 */
@RestController
@RequestMapping("/admin/user")
public class UserAdminController {

    @Resource
    private UserService userService;

    @RequestMapping("/list")
    public DataGridViewResult list(UserVo userVo){
        //设置分页
        PageHelper.startPage(userVo.getPage(), userVo.getLimit());
        //查询楼层列表
        List<User> userList = userService.findUserList(userVo);
        //创建分页对象
        PageInfo<User> userPageInfo = new PageInfo<User>(userList);

        return new DataGridViewResult(userPageInfo.getTotal(),userPageInfo.getList());
    }


    /**
     * 重置员工密码
     * @param id
     * @return
     */
    @RequestMapping("/resetPwd")
    public String resetPwd(String id){
        Map<String, Object> map = new HashMap<String, Object>();
        int i = userService.resetPwd(id);
        if(i>0){
            //添加员工失败
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"重置成功");
        }else{
            //添加员工失败
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"重置失败");
        }
        return JSON.toJSONString(map);
    }

    @RequestMapping("/delete")
    public String delete(String id){
        Map<String, Object> map = new HashMap<String, Object>();
        int i = userService.deleteUserById(id);

        if(i>0){
            //添加员工失败
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"删除成功");
        }else{
            //添加员工失败
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"删除失败");
        }
        return JSON.toJSONString(map);
    }

}
